package ord2.impl.notifier;

import java.io.File;
import java.net.URL;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedList;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import ord2.core.Parameter;
import ord2.core.Parametric;
import ord2.core.Result;
import ord2.notifier.ParametricNotifier;

/**
 *
 * @author José Ivan
 * @author Edviges
 */
public class EmailNotifier implements ParametricNotifier<String, File>, Parametric {

    private Properties pop = new Properties();
    private LinkedList<Parameter> pars = new LinkedList<Parameter>();
    private Session ses = null;
    private String from  = null;
    
    public EmailNotifier(Parameter[] pars) {
        
        String host = null;
        String port = "25";
        SmtpAuth auth = new SmtpAuth();

        this.pars.addAll(Arrays.asList(pars));
        
        for (Parameter p : this.pars) {
            
            String name = p.getName();
            if (name.equals("host")) {
                
                host = p.getValue();
            } else if (name.equals("password")) {
                
                auth.pass = p.getValue();
            } else if (name.equals("user")) {
                
                auth.user = p.getValue();
            } else if (name.equals("from")) {
                
                this.from = p.getValue();
            } else if (name.equals("port")) {

                port = p.getValue();
            }
        }
        
        pop.put("mail.host", host);
        pop.put("mail.smtp.auth", "true");
        pop.put("mail.smtp.port", port);
        
        ses = Session.getInstance(pop, auth);
    }
    
    @Override
    public boolean notify(String to, Result<File>[] res, Parameter[] pars) {
        
        try {
            
            StringBuilder sb = new StringBuilder();
            sb.append("Notificações achadas\n");
            for (Result<File> r : res) {
                
               sb.append(r.getValue()).append('\n');
            }
            sb.append("\n\n Acesse: ndou.pet.di.ufpb.br");
            MimeMessage msg = new MimeMessage(this.ses);
            msg.setFrom(new InternetAddress(this.from));
            msg.setRecipient(RecipientType.TO, new InternetAddress(to));
            msg.setSubject("Notificação Diário Ofical da União");
            msg.setSentDate(new Date());
            msg.setText(sb.toString());
            Transport.send(msg);
        } catch (Exception me) {
            
            me.printStackTrace();
            return false;
        }
        
        return true;
    }

    @Override
    public boolean notify(String to, Result<File>[] res) {
        
        return this.notify(to, res, null);
    }

    @Override
    public Parameter[] getParameters() {
        
        return this.pars.toArray(new Parameter[0]);
    }

    @Override
    public void addParameter(Parameter newParameter) {
        
        this.pars.add(newParameter);
    }

    @Override
    public void removeParameter(Parameter par) {
        
        this.pars.remove(par);
    }
    
    private static class SmtpAuth extends Authenticator {
        
        public String user;
        public String pass;
        
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            
            return new PasswordAuthentication(user, pass);
        }
    }
}
